package com.xneure.utils.http;

import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

import com.xneure.utils.JsonUtil;

public class FileUploadHandler extends HttpDebugger<MultipartEntityBuilder> {
	public FileUploadHandler(String urlServer) {
		super(HttpType.POST);
		setUrl(urlServer);
	}

	@Override
	protected HttpEntity loadEntity() {
		return getEntity().build();
	}

	public void addFile(String name, File file) {
		getEntity().addPart(name, new FileBody(file));
	}
	public void addData(String name,byte[] data){
		getEntity().addBinaryBody(name,data);
	}
	public void addTextBody(String name,Object data) {
		addTextBody(name,JsonUtil.toJson(data));
	}

	public void addTextBody(String name,String data) {
		getEntity().addTextBody(name, data);
	}
	@Override
	public MultipartEntityBuilder getEntity() {
		if (null == entity) {
			entity = MultipartEntityBuilder.create();
		}
		return entity;
	}
	
}
